import Vue from 'vue'
import Confirm from '@/components/wms/Message.vue'
import Vuetify from 'vuetify/lib'
const VueComponent = Vue.extend(Confirm)
// const vm = new VueComponent().$mount()
const instances = [];
let defaultOptions = {
    successIcon: 'check_circle',
    infoIcon: 'info',
    warningIcon: 'priority_high',
    errorIcon: 'warning',
    iconSize: 24,
    width: 350,
    maxWidth: '80%',
    className: '',
    okLabel: '确定',
    cancelLabel: '取消',
    transition: 'scroll-y-reverse-transition'
}
const isServer = typeof window === 'undefined';

const Message = function (options) {
    if (isServer) return;
    return new Promise((resolve) => {
        let vm = new VueComponent({
            el: document.createElement('div'),
            propsData: {
                ...defaultOptions,
                icon: defaultOptions[options.type + 'Icon'] || '',
                ...options
            }
        })
        const vuetifyObj = new Vuetify()
        vm.$vuetify = vuetifyObj.framework
        document.body.appendChild(vm.$el);
        vm.show = true
        if (vm.mode === 'prompt') {
            setTimeout(() => {
                vm.$el && vm.$el.querySelector('input').focus();
            }, 200);
        }
        instances.push(vm);
        vm.$on('close', function (result, value) {
            setTimeout(() => {
                vm.$el && vm.$el.parentNode && vm.$el.parentNode.removeChild(vm.$el);
                vm.$destroy();
                vm = null;
            }, 500);
            const index = instances.indexOf(vm);
            if (index !== -1) {
                instances.splice(index, 1);
            }
            return resolve({ result, value });
        })
    })
}

Message.config = function (options) {
    if (!options || Array.isArray(options) || typeof options !== 'object') return defaultOptions;
    for (const key in options) {
        // eslint-disable-next-line no-prototype-builtins
        if (!options.hasOwnProperty(key)) continue;
        defaultOptions[key] = options[key];
    }
    return defaultOptions;
};

Message.close = function () {
    instances.forEach((modal) => {
        modal.close(false);
    });
};

['alert', 'confirm', 'prompt'].forEach((mode) => {
    Message[mode] = function (content, options) {
        if (!content && arguments.length < 2) return;
        let title = '';
        switch (arguments.length) {
            case 1:
                options = {};
                break;
            case 2:
                if (typeof options === 'string') {
                    title = options;
                    options = {};
                }
                break;
            default:
                // eslint-disable-next-line prefer-rest-params
                title = arguments[1];
                // eslint-disable-next-line prefer-rest-params
                options = arguments[2];
                break;
        }
        return Message({
            title,
            content,
            ...options,
            mode: mode
        });
    };
});

Message.install = function (Vue, options) {
    Message.config(options);
    Vue.prototype.$message = Message;
    Vue.prototype.$alert = Message.alert;
    Vue.prototype.$confirm = Message.confirm;
    Vue.prototype.$prompt = Message.prompt;
};


export default Message;